package com.yin.jia.liang.controller;

import com.github.pagehelper.PageInfo;
import com.qiniu.util.Auth;
import com.yin.jia.liang.common.Config;
import com.yin.jia.liang.entity.Goods;
import com.yin.jia.liang.entity.User;
import com.yin.jia.liang.service.GoodsPlService;
import com.yin.jia.liang.service.GoodsService;
import com.yin.jia.liang.vo.GoodsPlVo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    GoodsPlService goodsPlService;

    @RequestMapping(value = "{page}", method = RequestMethod.GET)
    public String list(@PathVariable Integer page, @ModelAttribute Goods goods, Model model) {
        PageInfo<Goods> pageInfo = goodsService.pageFindByExample(page, goods);
        model.addAttribute("page", pageInfo);
        return "goods/list";
    }

    @RequestMapping(value = "store/{page}", method = RequestMethod.GET)
    public String store(@PathVariable Integer page, @ModelAttribute Goods goods, Model model) {
        PageInfo<Goods> pageInfo = goodsService.pageFindByExample(page, goods);
        model.addAttribute("page", pageInfo);
        return "goods/store";
    }

    @RequestMapping(value = "show/{id}", method = RequestMethod.GET)
    public String show(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        List<GoodsPlVo> vo = goodsPlService.findByGoodsId(id);
        model.addAttribute("pl", vo);
        model.addAttribute("goods", goods);
        return "goods/show";
    }

    @RequestMapping(value = "show2/{id}", method = RequestMethod.GET)
    public String show2(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        List<GoodsPlVo> vo = goodsPlService.findByGoodsId(id);
        model.addAttribute("pl", vo);
        model.addAttribute("goods", goods);
        return "goods/show2";
    }

    @RequestMapping(value = "show3/{id}", method = RequestMethod.GET)
    public String show3(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        List<GoodsPlVo> vo = goodsPlService.findByGoodsId(id);
        model.addAttribute("pl", vo);
        model.addAttribute("goods", goods);
        return "goods/show3";
    }

    @RequestMapping(value = "show4/{id}", method = RequestMethod.GET)
    public String show4(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        List<GoodsPlVo> vo = goodsPlService.findByGoodsId(id);
        model.addAttribute("pl", vo);
        model.addAttribute("goods", goods);
        return "goods/show4";
    }


    @RequestMapping(value = "buy/{id}", method = RequestMethod.GET)
    public String buy(@PathVariable Integer id, Model model) {
        Goods goods = goodsService.findById(id);
        model.addAttribute("goods", goods);
        return "goods/buy";
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String index(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        if ("admin".equals(user.getUsername())) {
            return "goods/add";
        }
        return "redirect:/";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String index(@ModelAttribute Goods goods, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        if ("admin".equals(user.getUsername())) {
            goodsService.save(goods);
        }
        return "redirect:/";
    }

    @RequestMapping(value = "del/{id}", method = RequestMethod.GET)
    public String del(@PathVariable Integer id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }
        if ("admin".equals(user.getUsername())) {
            goodsService.delete(id);
        }
        return "redirect:/";
    }
}
